﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Windows.Forms;
using HomeCare.View;
using System.Data;

namespace HomeCare.Model
{
   public class MedicamentoDados
    {
       
       Conexao con = new Conexao();

        public bool InsereMedicamento(Medicamento medicamento)
        {
       
            SqlConnection sql = con.Conectar();
            sql.Open();
            string query = String.Format("insert into Medicamentos (Medicamento, Classificacao) values ('{0}','{1}')", medicamento.medicamento, medicamento.classificacao);
            try
            {
                SqlCommand cmd = new SqlCommand(query, sql);
                cmd.ExecuteNonQuery();
                cmd.Dispose();
                sql.Close();
                return true;
            }
            catch (Exception ex)
            {
                
                //throw new Exception("Erro ao Inserir Medicamento.");
                sql.Close();
                MessageBox.Show(ex.Message);
                return false;
            }
        }


        public bool AlteraMedicamento(Medicamento medicamento)
        {
            SqlConnection sql = con.Conectar();
            sql.Open();
            string query = String.Format("update Medicamentos set Medicamento='{0}', Classificacao='{1}' where id='{2}'", medicamento.medicamento, medicamento.classificacao, medicamento.id);
            try
            {
                SqlCommand cmd = new SqlCommand(query, sql);
                cmd.ExecuteNonQuery();
                cmd.Dispose();
                sql.Close();
                return true;
            }
            catch (Exception ex)
            {
                //throw new Exception("Erro ao Alterar Medicamento.");
                sql.Close();
                MessageBox.Show(ex.Message);
                return false;
            }
        }


        public bool ExcluiMedicamento(Medicamento medicamento)
        {
            SqlConnection sql = con.Conectar();
            sql.Open();
            string query = String.Format("delete from Medicamentos where Id='{0}'", medicamento.id);
            try
            {
                SqlCommand cmd = new SqlCommand(query, sql);
                cmd.ExecuteNonQuery();
                cmd.Dispose();
                sql.Close();
                return true;
            }
            catch (Exception ex)
            {
                //throw new Exception("Erro ao Alterar Medicamento.");
                sql.Close();
                MessageBox.Show(ex.Message);
                return false;
            }
        }

        public DataSet ConsultaMedicamento(Medicamento medicamento)
        {
            SqlConnection sql = con.Conectar();
            sql.Open();
            string query = "select * from Medicamentos where (1=1) ";
            query += String.IsNullOrEmpty(medicamento.classificacao)? String.Empty :( String.Format(" and Classificacao like '%{0}%' ", medicamento.classificacao));
            query += String.IsNullOrEmpty(medicamento.medicamento) ? String.Empty: (String.Format(" and Medicamento like '%{0}%' ", medicamento.medicamento));
            query += " order by Medicamento ";
            try
            {
                SqlDataAdapter dataAdapter = new SqlDataAdapter(query, sql);
                DataSet dataSet = new DataSet();
                dataAdapter.Fill(dataSet);
                sql.Close();
                return dataSet;
                
            }
            catch (Exception ex)
            {
                sql.Close();
                throw new Exception("Erro ao Consultar Medicamento.");
                //MessageBox.Show(ex.Message);
                
            }
        }

        public DataSet RelatorioMedicamentos()
        {
            SqlConnection sql = con.Conectar();
            sql.Open();
            string query = "select * from Medicamentos where (1=1) ";
            query += " order by Medicamento ";
            try
            {
                SqlDataAdapter dataAdapter = new SqlDataAdapter(query, sql);
                DataSet dataSet = new DataSet();
                dataAdapter.Fill(dataSet);
                sql.Close();
                return dataSet;

            }
            catch (Exception ex)
            {
                sql.Close();
                throw new Exception("Erro ao Consultar Medicamento.");
                //MessageBox.Show(ex.Message);

            }
        }


    }
}


